package com.design.twords.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.design.twords.R;
import com.design.twords.fragment.FeekBackFragment;
import com.design.twords.fragment.HelpFragment;
import com.design.twords.utils.MyApplication;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * Create by Alen
 * Time:  19:56
 */
public class HelpActivity extends AppCompatActivity {
    private ImageView back;
    private TabLayout tabLayout;
    private TabItem help, feedback;
    private ViewPager viewPager;
    private Fragment[] fragments;
    private int itemWhat = 0;

    private String[] titles = new String[]{"帮助", "反馈"};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        MyApplication.getInstance().addActivity(this);
        initView();

        fragments = new Fragment[]{new HelpFragment(), new FeekBackFragment()};

        initViewPager();
        initTabLayout();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                finish();
            }
        });


    }

    private void initTabLayout() {
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    private void initViewPager() {
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        });

    }

    private void initView() {
        tabLayout = findViewById(R.id.help_tablayout);
        viewPager = findViewById(R.id.help_viewpager);
        back = findViewById(R.id.help_back);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().removeActivity(this);
    }
}
